package org.homeorganiser.flex.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.homeorganiser.flex.dto.Book;
import org.homeorganiser.flex.util.Constant;

public class BookDAO implements IBookDAO {

	private final DataSource dataSource;
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement ps;

	public BookDAO(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public List<Book> findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public Book findById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Book create(Book bookobj) {
		try {
			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_INSERT_BOOK_TABLE,
					new String[] { "ID" });
			
			ps.setString(1, bookobj.getBookname());
			ps.setString(2, bookobj.getAuthor());
			ps.setString(3, bookobj.getPrice());
			ps.setString(4, bookobj.getComments());			
			ps.setInt(5, bookobj.getPublicationId());
			ps.setInt(6, bookobj.getTypeId());
			ps.setInt(7, bookobj.getGenreId());
			ps.setInt(8, bookobj.getLanguageId());
			ps.setInt(9, bookobj.getQualityId());
			ps.setInt(10, bookobj.getLocationId());
			ps.setInt(11, bookobj.getSubCategoryId());
			ps.setInt(12, bookobj.getCategoryId());
			ps.setInt(13, bookobj.getInventorytype());
			ps.setInt(14, bookobj.getIsActive());
			ps.setString(15, bookobj.getAddedBy());

			ps.executeUpdate();
			ResultSet rs = ps.getGeneratedKeys();
			rs.next();
			// Update the id in the returned object. This is important as this
			// value must be returned to the client.
			int id = rs.getInt(1);
			bookobj.setId(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return bookobj;
	}

	public boolean update(Book item) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean remove(Book item) {
		// TODO Auto-generated method stub
		return false;
	}

	public List<Book> findAll(int type) {
		List<Book> bookList = new ArrayList<Book>();

		Book bookobj;
		try {

			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_SELECT_ALL_BOOK);
			ps.setInt(1, type);
			rs = ps.executeQuery();
			
			while (rs.next()) {
				bookobj = new Book();
				bookobj.setId(rs.getInt("id"));
				bookobj.setBookname(rs.getString("bookname"));
				bookobj.setAuthor(rs.getString("authorname"));
				bookobj.setPrice(rs.getString("price"));
				bookobj.setComments(rs.getString("comments"));
				bookobj.setPublicationId(rs.getInt("publicationid"));
				bookobj.setTypeId(rs.getInt("typeid"));
				bookobj.setGenreId(rs.getInt("genreid"));
				bookobj.setQualityId(rs.getInt("qualityid"));
				bookobj.setLocationId(rs.getInt("locationid"));
				bookobj.setSubCategoryId(rs.getInt("subcategoryid"));
				bookobj.setCategoryId(rs.getInt("categoryid"));
				bookobj.setInventorytype(rs.getInt("inventorytype"));
				bookobj.setIsActive(rs.getInt("isActive"));
				bookobj.setAddedBy(rs.getString("addedBy"));
				
				bookList.add(bookobj);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return bookList;
	}

	public boolean removeAll(int type) {
		// TODO Auto-generated method stub
		return false;
	}

}
